import { request } from '@/utils/request'
import type { IAnswer, IResponse } from '.'
import type { IDialogData, IPageResp, MainItem, PdfFile } from './type'
import type { LANGUAGE_CODE } from '@/const'
const PREFIX_CHAT = '/chat/copilotDialog'

export function addQuestion(data: IDialogData) {
  return request<IResponse<IAnswer>>({
    url: `${PREFIX_CHAT}/frontend/addQuestion`,
    method: 'post',
    data
  })
}

export function saveOrUpdate(data: Partial<IDialogData>) {
  if (!data.id) { // id 为空，删除id 字段
    delete data.id
  }
  return request<IResponse>({
    method: 'post',
    url: `${PREFIX_CHAT}/save-or-update`,
    data
  })
}

interface IPageParam {
  pageSize: string
  userId?: string
  time?: number
}

export function page(params?: IPageParam) {
  if(params)params.time = new Date().getTime()
  return request<IPageResp<IDialogData>>({
    method: 'get',
    url: `${PREFIX_CHAT}/page`,
    params
  })
}

export function remove(id: string) {
  return request({
    method: 'get',
    url: `${PREFIX_CHAT}/remove/${id}`
  })
}

// 事件分页列表
export const apiNewsList = (params = {}) => {
  return request({
    url: `${PREFIX_CHAT}/chatEvent/frontend/list`,
    method: 'GET',
    params
  })
}

// 事件详情
export const apiNewsDetail = (params = {}) => {
  return request({
    url: `${PREFIX_CHAT}/chatEvent/frontend/detail`,
    method: 'GET',
    params
  })
}

// 首页更新提醒(数量)
export const apiGetRenewals = (renewalTime: string) => {
  return request({
    url: `${PREFIX_CHAT}/chatEvent/frontend/getRenewals`,
    method: 'GET',
    params: {
      renewalTime
    }
  })
}

// 相关事件分页列表
export const apiNewsRelaEvents = (params = {}) => {
  return request({
    url: `${PREFIX_CHAT}/chatEvent/frontend/relatedChatEvents`,
    method: 'GET',
    params
  })
}

// 相同事件分页列表
export const apiNewsSameEvents = (params = {}) => {
  return request({
    url: `${PREFIX_CHAT}/chatEvent/frontend/sameChatEvents`,
    method: 'GET',
    params
  })
}

// 事件原文详情
export const apiNewsOriginDetail = (params = {}) => {
  return request({
    url: `${PREFIX_CHAT}/chatEvent/frontend/articleDetail`,
    method: 'GET',
    params
  })
}

// 事件 订阅
export const apiNewsSubscribe = (data = {}) => {
  return request({
    url: `${PREFIX_CHAT}/chatUserSubscribe/subscribe`,
    method: 'POST',
    data
  })
}

// 获取订阅状态
export const apiUserSubscribeState = (params = {}) => {
  return request({
    url: `${PREFIX_CHAT}/chatUserSubscribe/user/subscribeState`,
    method: 'POST',
    params
  })
}

/**
 * 获取早报
 * @param {*} createTime
 * @returns
 */
export const apiGetMorningNews = (createTime: string) => {
  return request({
    url: `${PREFIX_CHAT}/chatUserSubscribe/getMorningPaperList`,
    method: 'POST',
    data: {
      createTime
    }
  })
}

/**************************** 事件接口 end ******************************/

/**************************** 聊天室 start ******************************/

export enum ASK_TYPE {
  normal = 0,
  enhence = 1
}

interface IAsk {
  audioDuration?: string
  dialogId: string
  sessionId: string
  questionId: string
  questionUrl?: string

  //搜索增强
  type: ASK_TYPE
}
//发送一条提问
export const apiAskGPT = (data: IAsk) => {
  return request<
    IResponse<{
      id: string
      questionId: string
      questionContent: string
      createdTimestamp: number
    }>
  >({
    url: `${PREFIX_CHAT}/frontend/addQuestion`,
    method: `POST`,
    data
  })
}
interface stopQuestionParams {
  dialogId: string,
      sessionId: string,
      questionId: string,
      answerContent: string
}
export const apiStopQuestion = (data: stopQuestionParams)=>{
  return request({
    url: `${PREFIX_CHAT}/frontend/stopQuestion`,
    method: `POST`,
    data
  })
}
interface IbehavioData {
  dialogId?: string
  answerId?: string
  // 1-点赞，2-踩，3-分享 4-清理聊天记录
  behaviorType: number
}
//回答的 点赞 踩
export const apiAnswerFeedback = (data: IbehavioData) => {
  return request({
    url: `${PREFIX_CHAT}/frontend/edit/behavior`,
    method: `POST`,
    data
  })
}

interface IShareRecorder {
  copilotConversationShareReqs: {id: string, shareType: number}[]
}
// 分享聊天记录
export const apiShareRecorder = (data: IShareRecorder) => {
  return request({
    url: `${PREFIX_CHAT}/frontend/share/recorder`,
    method: `POST`,
    data: data.copilotConversationShareReqs
  })
}

// 查询分享聊天记录
export const apiShareRecorderList = (id: string) => {
  return request({
    url: `${PREFIX_CHAT}/frontend/share/recorder/byId`,
    method: `GET`,
    params: { id }
  })
}
interface IHistoryData {
  current: number
  customerId: string
  dialogId: string
  limit: number
}

//问答聊天列表
export const apiChatHistory = (data: IHistoryData) => {
  return request<IPageResp<MainItem>>({
    url: `${PREFIX_CHAT}/frontend/pageList`,
    method: `POST`,
    data
  })
}


export interface recommand {
  id: string
  questionContent: string
}

//第一次使用的推荐问题
export const apiFirstUserPreQuestions = (params = {}) => {
  return request<IPageResp<recommand>>({
    url: `${PREFIX_CHAT}/recommendQuestion/frontend/list`,
    method: `GET`,
    params
  })
}

//生成分享的id
export const apiGenerateShareId = (
  data: {
    id: 'string'
    type: 0
  }[]
) => {
  return request({
    url: `${PREFIX_CHAT}/frontend/share/recorder`,
    method: `POST`,
    data
  })
}

//获取分享的聊天记录
export const apiSharedChatList = (params: { id: string }) => {
  return request({
    url: `${PREFIX_CHAT}/frontend/share/recorder/byId`,
    method: `GET`,
    params
  })
}

/**************************** 聊天室 end ******************************/

/**
 * 是否有问答权限
 * @returns
 */
export const checkAuthority = (userId: string) => {
  return request({
    url: `${PREFIX_CHAT}/userQuestionAuthority/checkAuthority`,
    method: `GET`,
    params: {
      userId
    }
  })
}

/**
 * 是否有问答权限
 * @returns
 */
export const generateInvitationCode = (params: any) => {
  return request({
    url: `${PREFIX_CHAT}/userQuestionAuthority/generateInvitationCode`,
    method: `GET`,
    params
  })
}

/**
 *
 * @returns 验证邀请码码
 */
export const validateInviteCode = (code: string, userId: string) => {
  return request({
    url: `${PREFIX_CHAT}/userQuestionAuthority/checkInvitationCode`,
    method: `GET`,
    params: {
      invitationCode: code,
      userId
    }
  })
}
/**
 *
 * @returns 获取邀请码
 */
export const getUserInvitationInfo = (userId: string) => {
  return request({
    url: `${PREFIX_CHAT}/userQuestionAuthority/getUserInvitationInfo`,
    method: `GET`,
    params: {
      userId
    }
  })
}

/**
 * 获取当前用户的问答次数
 * @param {*} params
 * @returns
 */
export const getQuestionCount = (userId: string) => {
  return request({
    url: `${PREFIX_CHAT}/frontend/getUserConversationCount`,
    method: 'GET',
    params: {
      userId
    }
  })
}

/**
 * 获取问答过期时间
 * @param {*} params
 * @returns
 */
export const apiGetReplyExpireTime = () => {
  return request({
    url: `${PREFIX_CHAT}/mstDialog/copilotReplyExpiredTime`,
    method: 'GET',
  })
}

interface IPerformance {
  performanceDescription: string
  performanceMax: string
  performanceMin: string
  performanceName: string
  performanceType: string
  performanceUnit: string
}
interface IMatchProduct {
  productEnterprise: string
  productName: string
}
export interface IMaterial {
  childType: string
  id: string
  matchProduct: IMatchProduct[]
  materialFeature: string
  materialNameCh: string
  materialNameEn: string
  materialNameSimple: string
  materialUsage: string
  materialType: string
  paragraphId: string[]
  performance: IPerformance[]
  preprocessMethod: string
}

/**
 * 获取材料列表
 * @returns
 */
// export const getMstMaterialPaperListUsingPOST = (ids: string) => {
//   return request<IMaterial>({
//     url: `${PREFIX_CHAT}/getMstMaterialPaperList`,
//     method: 'GET',
//     params: {
//       ids
//     }
//   })
// }

interface IDisciplineDiscipline {
  majorDiscipline: string
  subDiscipline: string
}
interface ChartList{
  url:string
  caption: string
}
export interface IMstPaper {
  index?:number

  author: string
  createTime: string

  discipline: IDisciplineDiscipline[]
  doi: string
  journal: string
  llmQuestion: string[]
  matchKeyword: string[]
  paperId: string
  pdfId: string
  pdfUrl: string
  sourceLink: string
  releaseTime: string
  researchConclusion: string
  researchContent: string
  researchMethod: string
  researchObjective: string
  researchProspect: string
  researchResult: string
  title: string
  updateTime: string
  //改性方法
  modificationName: string
  documentType: string
  mstMaterialPaperList: IMaterial[]
  mstChartList: ChartList[]
  isPublicAccess: string
  titleCh: string
  researchContentCh: string
  languageCode: LANGUAGE_CODE
}


export interface IConditions {
  // 学科领域
  discipline?: string
  //实验方法 使用方法同ids
  experimentMethod?: string
  // 材料名称
  materialNameCh?: string
  // 实验方法
  materialType?: string
  // 实验方法
  modificationName?: string
  //实验方法
  performanceType?: string
}

export interface IParams extends IConditions {
  current: number
  ids: string
  limit: number
  // 学科领域
  discipline?: string
  //实验方法 使用方法同ids
  experimentMethod?: string
  // 材料名称
  materialNameCh?: string
  // 实验方法
  materialType?: string
  // 实验方法
  modificationName?: string
  //实验方法
  performanceType?: string
}

/**
 * 获取论文列表
 * @returns
 */

export const getMstPaperList = (params: IParams) => {
  return request<IPageResp<IMstPaper>>({
    url: `${PREFIX_CHAT}/getMstPaperList`,
    method: 'get',
    params
  })
}

export interface IConditionData {
  disciplineList: string[]
  experimentMethodList: string[]
  materialNameChList: string[]
  materialTypeList: string[]
  modificationNameList: string[]
  performanceTypeList: string[]
}
/**
 *
 * @param ids 论文ids
 * @returns 论文参数列表
 */
export function getConditionData(ids: string) {
  return request<IResponse<IConditionData>>({
    url: `${PREFIX_CHAT}/getMstQueryCondition`,
    method: 'get',
    params: {
      ids
    }
  })
}

export interface DialogDetail {
  fileSize : string,
  fileName : string,
  dialogName : string,
  fileUrl:	string,
  id:	string,
  pdfId:	string,
  processStatus: number, // 1:待上传 2:上传完成 3:待解析 4:解析中5:正文提取完成 6:解析完成
  queryType:	number, // 1对话搜索 2单篇文档对话搜索
searchType:	number, // 搜索类型 0 即答 1全部 2为论文 3为专利
documentType?:	number, // 1论文 2专利
}
export function apiGetDialogDetail(id: string) {
  return request<IResponse<DialogDetail>>({
    url: `${PREFIX_CHAT}/detail/`+ id,
    method: 'get',
  })
}


